package com.ck.controller.student;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ck.domain.StudentExam;
import com.ck.domain.StudentPaper;
import com.ck.service.ExamService;

public class CheckPaperController extends AbstractController {

	private ExamService examService;
	
	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		StudentExam studentExam = new StudentExam();
		StudentPaper studentPaper = new StudentPaper();
		
		Integer studentID = (Integer) request.getSession().getAttribute("studentID");
		Integer paperID = Integer.parseInt(request.getParameter("paperID"));
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		
		//学生交卷了
		studentExam.setStudent(studentID);
		studentExam.setExam(examID);
		examService.editStudentExamForHandInPaper(studentExam);
		
		//开始改卷
		Integer total_score = Integer.parseInt(request.getParameter("total_score"));
		Integer score = 0;
		Integer get_score = null;
		String wrong_panduan_list = "";
		String wrong_xuanze_list = "";
		String wrong_tiankong_list = "";
		
		//批改选择题
		Integer panduan_num = Integer.parseInt(request.getParameter("panduan_num"));
		for(int i=0; i<panduan_num; i++){
			//获取题目
			Integer panduanID = Integer.parseInt(request.getParameter("panduanID_"+i));
			//获取用户选择的答案
			String panduan_answer_str = request.getParameter("panduan_answer_"+i);
			//批改
			get_score = examService.checkPanduan(panduanID, panduan_answer_str, studentID, paperID);
			
			if( get_score == 0){
				//答错了
				wrong_panduan_list = wrong_panduan_list + panduanID + ",";
			}else{
				score = score + get_score;
			}
		}
		
		//批改选择题
		Integer xuanze_num = Integer.parseInt(request.getParameter("xuanze_num"));
		for(int i=0; i<xuanze_num; i++){
			//获取题目
			Integer xuanzeID = Integer.parseInt(request.getParameter("xuanzeID_"+i));
			//获取用户选择的答案
			String xuanze_answer = request.getParameter("xuanze_answer_"+i);
			//批改
			get_score = examService.checkXuanze(xuanzeID, xuanze_answer, studentID, paperID);
			
			if( get_score == 0){
				//答错了
				wrong_xuanze_list = wrong_xuanze_list + xuanzeID + ",";
			}else{
				score = score + get_score;
			}
		}
		
		//批改填空题
		Integer tiankong_num = Integer.parseInt(request.getParameter("tiankong_num"));
		for(int i=0; i<tiankong_num; i++){
			//获取题目
			Integer tiankongID = Integer.parseInt(request.getParameter("tiankongID_"+i));
			//获取用户选择的答案
			String tiankong_answer = request.getParameter("tiankong_answer_"+i);
			//批改
			get_score = examService.checkTiankong(tiankongID, tiankong_answer, studentID, paperID);
			
			if( get_score == 0){
				//答错了
				wrong_tiankong_list = wrong_tiankong_list + tiankongID + ",";
			}else{
				score = score + get_score;
			}
		}

		//整理成绩和错题等信息
		Date now = new Date();
		studentPaper.setCst_create(now);
		studentPaper.setCst_modify(now);
		studentPaper.setStudent(studentID);
		studentPaper.setPaper(paperID);
		studentPaper.setScore(score);
		studentPaper.setExtra_score(null);	//暂时不开放附加分功能。
		//判断是否及格(大于等于总分的60%)
		if( score < total_score*0.6 ){
			studentPaper.setPass(0);	//0表示不及格；
		}else{
			studentPaper.setPass(1);	//1表示及格；
		}
		studentPaper.setWrong_panduan_list(wrong_panduan_list);
		studentPaper.setWrong_xuanze_list(wrong_xuanze_list);
		studentPaper.setWrong_tiankong_list(wrong_tiankong_list);
		studentPaper.setWrong_zhuguan_list(null);	//没有附加题
		//写入数据库
		examService.addNewStudentPaper(studentPaper);
		
		Map model = new HashMap();
		model.put("score", score);
		return new ModelAndView("student/checkPaper", model);
	}

}
